%% Set up the Import Options and import the data
opts = spreadsheetImportOptions("NumVariables", 13);

% Specify sheet and range
opts.Sheet = "Sheet1";
opts.DataRange = "A2:M65";

% Specify column names and types
opts.VariableNames = ["Source", "Sink", "words", "nonwords", "Difference", "psame", "Sig", "VarName8", "k", "kalpham", "FDRSig", "pmk", "FDRadj"];
opts.VariableTypes = ["string", "string", "double", "double", "double", "double", "string", "string", "double", "double", "string", "double", "double"];

% Specify variable properties
opts = setvaropts(opts, "VarName8", "WhitespaceRule", "preserve");
opts = setvaropts(opts, ["Source", "Sink", "Sig", "VarName8", "FDRSig"], "EmptyFieldRule", "auto");

% Import the data
LexicalityDecodingROIs400500mswordsnonwordsFDRcorrected1 = readtable("/autofs/space/clive_001/users/olivia/GE_new/Granger/results/Lexicality_Decoding_ROIs_downstream_400-600ms_words-nonwords_FDRcorrected.xlsx", opts, "UseExcel", false);


%% Clear temporary variables
clear opts

% Convert Table to Struct
pvalstruct = table2struct(LexicalityDecodingROIs400500mswordsnonwordsFDRcorrected1);

%Ordered ROI list for arranging the matrix
srcROIs = {'L-STG_1', 'L-MTG_2', 'R-STG_1', 'R-STG_3', 'R-STG_2', 'R-MTG_1', 'R-MTG_2', 'R-ITG_1', 'R-ITG_2', 'R-postCG_2'};
snkROIs = {'L-STG_2', 'L-MTG_1', 'L-TPol_1', 'L-ITG_1', 'L-ITG_2', 'L-SPC_1', 'L-SMG_1', 'L-postCG_1', 'L-SFG_1', 'L-cMFG_1', 'L-ParsTri_1','L-ParsOrb_1', 'L-CC_1', 'L-ParaHip_1',...
    'R-MTG_3', 'R-MTG_4', 'R-ITG_3',  'R-AG_1', 'R-postCG_5', 'R-postCG_1', 'R-postCG_3', 'R-postCG_4', 'R-preCG_1', 'R-preCG_2', 'R-SFG_1', 'R-SFG_3', 'R-SFG_2', 'R-ParsOrb_1', 'R-LOC_1'};
pubsrcROIs = {'L-STG_1', 'L-MTG_1', 'R-STG_1', 'R-STG_2', 'R-STG_3', 'R-MTG_2', 'R-MTG_3', 'R-ITG_2', 'R-ITG_3', 'R-postCG_5'};
pubsnkROIs = {'L-STG_2', 'L-MTG_2', 'L-TPol_1', 'L-ITG_1', 'L-ITG_2', 'L-SPC_1', 'L-SMG_1', 'L-postCG_1', 'L-SFG_1', 'L-cMFG_1', 'L-ParsTri_1', 'L-ParsOrb_1', 'L-CC_1', 'L-ParaHip_1',...
    'R-MTG_1', 'R-MTG_4', 'R-ITG_1', 'R-AG_1', 'R-postCG_1', 'R-postCG_2', 'R-postCG_3', 'R-postCG_4', 'R-preCG_1', 'R-preCG_2', 'R-SFG_1', 'R-SFG_2', 'R-SFG_3', 'R-ParsOrb_1', 'R-LOC_1'};
%make the colormap
riter = (1-(216/255))/15;
giter = (1-(27/255))/15;
biter = (1-(96/255))/15;
pinkwhite = [216/255, 27/255, 96/255];
for k = 1:15
pinkwhite(k+1, :) = pinkwhite(k, :) + [riter, giter, biter];
end
riter_blue = (1-(30/255))/15;
giter_blue = (1-(136/255))/15;
biter_blue = (1-(229/255))/15;
bluewhite = [1,1,1];
for k = 1:15
bluewhite(k+1, :) = bluewhite(k, :) - [riter_blue, giter_blue, biter_blue];
end
redblue = [pinkwhite; bluewhite];

% Build the matrix form of it
arrayform = [];
x = []; y = []; c = [];
for kcomp = 1:length(pvalstruct)
if matches(pvalstruct(kcomp).FDRSig, '*')
srcROIidx = find(matches(srcROIs, pvalstruct(kcomp).Source));
sinkROIidx = find(matches(snkROIs, pvalstruct(kcomp).Sink));
arrayform(srcROIidx, sinkROIidx) = pvalstruct(kcomp).Difference;
valence = sign(pvalstruct(kcomp).Difference);
x = horzcat(x, [sinkROIidx-0.5; sinkROIidx; sinkROIidx+0.5]);
y = horzcat(y, [srcROIidx+(0.5*valence); srcROIidx-(0.5*valence); srcROIidx+(0.5*valence)]);
c = [c; pvalstruct(kcomp).Difference];
end
end
figure; image(arrayform, 'CDataMapping', 'scaled');
colormap(single(redblue));
set(gca, 'CLim', [-max(abs(arrayform(:))), max(abs(arrayform(:)))]);
set(gca, 'xaxislocation', 'top');
set(gca, 'XTick', [1:length(snkROIs)], 'XTickLabels', pubsnkROIs, 'YTick', [1:10], 'YTickLabels', pubsrcROIs);
xlabel('Sink'); ylabel('Source'); colorbar;

figure; patch(x, y, c, 'CDataMapping', 'scaled'); colormap(single(redblue));
set(gca, 'YDir', 'reverse');
set(gca, 'xaxislocation', 'top');
xlabel('Sink'); ylabel('Source');
cb = colorbar('eastoutside', 'Direction', 'normal'); cb.Label.String = '$$ \textsf{Dif{}ference (words} \bigtriangleup \textsf{- nonwords} \bigtriangledown \textsf{)} $$'; cb.Label.Interpreter = 'latex'; cb.Label.Rotation = 270; cb.Label.VerticalAlignment = 'bottom';
set(gca, 'XTick', [1:length(snkROIs)], 'XTickLabels', pubsnkROIs, 'YTick', [1:length(pubsrcROIs)], 'YTickLabels', pubsrcROIs);
set(gca, 'XLim', [0.5, length(snkROIs)+0.5], 'YLim', [0.5, 10.5]);
set(gca, 'CLim', [-max(abs(arrayform(:))), max(abs(arrayform(:)))]);
box on;

%% Words only heatmap
wordsonlyarray = [];
for kcomp = 2:length(pvalstruct)
srcROIidx = find(matches(srcROIs, cellstr(pvalstruct(kcomp).Source)));
sinkROIidx = find(matches(snkROIs, cellstr(pvalstruct(kcomp).Sink)));
wordsonlyarray(srcROIidx, sinkROIidx) = pvalstruct(kcomp).words;
end
figure; imagesc(wordsonlyarray);
colormap('hot'); %uses matlab preset colormap
set(gca, 'xaxislocation', 'top');
set(gca, 'XTick', [1:length(snkROIs)], 'XTickLabels', snkROIs, 'YTick', [1:10], 'YTickLabels', srcROIs);
xlabel('Sink'); ylabel('Source'); colorbar;